/*
 * @Description: 
 * @Date: 2024-03-13 22:47:10
 * @LastEditTime: 2024-05-09 23:55:03
 * @FilePath: \yh_fe_db_portal\src\router\beforeEach.js
 */
const routes1 = [
  {
    path: '/',
    redirect: { name: 'project' }
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('../page/Login'),
    meta: {
      needLogin: false,
      menuIndex: 'login'
    }
  },
  {
    name: 'home',
    path: '/home',
    component: () => import('../page/Home'),
    meta: {
      needLogin: true,
      menuIndex: 'home'
    }
  },
  {
    name: 'project',
    path: '/project',
    component: () => import('@/page/Project'),
    redirect: {
      name: 'list'
    },
    meta: {
      needLogin: true,
      menuIndex: 'project',
      title: '项目管理'
    },
    children: [
      {
        name: 'list',
        path: 'list',
        component: () => import('@/page/Project/List'),
        meta: {
          perms: 'bigdata:project:list'
        }
      },
      {
        path: ':projectId',
        component: () => import('@/page/Project/Layout'),
        meta: {
          needLogin: true,
          menuIndex: 'project',
          title: '项目管理'
        },
        children: [
          {
            name: 'dataSource',
            path: 'dataSource',
            component: () => import('@/page/Project/Layout/DataSource'),
            meta: {
              needLogin: true,
              menuIndex: 'dataSource',
              title: '数据源',
              icon: 'project_menu_dataSource',
              perms: 'bigdata:project:source:list'
            }
          },
          {
            // name: 'notebook',
            // path: '',
            // component: () => import('@/page/Project/Layout/WorkSpace'),
            // meta: {
            //   needLogin: true,
            //   menuIndex: 'Workspace',
            //   title: '工作区',
            // },
            // children: [
            //   {
            //     name: 'workFolder',
            //     path: 'workFolder/:uniq?',
            //     component: () => import('@/page/Workspace'),
            //     meta: {
            //       needLogin: true,
            //       menuIndex: 'workFolder',
            //       title: '工作目录',
            //     },
            //   },
            //   {
            //     name: 'workFlow',
            //     path: 'workFlow',
            //     component: () => import('@/page/WorkFlow'),
            //     meta: {
            //       needLogin: true,
            //       menuIndex: 'workFlow',
            //       title: '工作流',
            //     },
            //   }
            // ]
            name: 'workFolder',
            path: 'workFolder',
            component: () => import('@/page/Workspace'),
            meta: {
              needLogin: true,
              menuIndex: 'workFolder',
              title: '工作区',
              icon: 'project_menu_workbench',
              perms: 'bigdata:project:work'
            }
          },
          // {
          //   // name: 'notebook',
          //   path: '',
          //   component: () => import('@/page/Project/Layout/WorkSpace'),
          //   meta: {
          //     needLogin: true,
          //     menuIndex: 'Workspace',
          //     title: '工作区',
          //   },
          //   children: [
          //     {
          //       name: 'workFolder',
          //       path: 'workFolder/:uniq?',
          //       component: () => import('@/page/Workspace'),
          //       meta: {
          //         needLogin: true,
          //         menuIndex: 'workFolder',
          //         title: '工作目录',
          //       },
          //     },
          //     {
          //       name: 'workFlow',
          //       path: 'workFlow',
          //       component: () => import('@/page/WorkFlow'),
          //       meta: {
          //         needLogin: true,
          //         menuIndex: 'workFlow',
          //         title: '工作流',
          //       },
          //     }
          //   ]
          // },
          {
            // name: 'dataQuery',
            path: '',
            component: () => import('@/page/Project/Layout/DataQuery'),
            meta: {
              needLogin: true,
              menuIndex: 'dataQuery',
              title: '数据查询',
              icon: 'project_menu_dataFind',
              perms: 'bigdata:project:dql:list'
            },
            children: [
              {
                name: 'apiList',
                path: 'apiList',
                component: () => import('@/page/Project/Layout/DataQuery/ApiList'),
                meta: {
                  perms: 'bigdata:project:dql:category:list',
                  needLogin: true,
                  menuIndex: 'apiList',
                  title: 'API管理'
                }
              },
              {
                name: 'apiMonitor',
                path: 'apiMonitor',
                component: () => import('@/page/Project/Layout/DataQuery/ApiMonitor'),
                meta: {
                  perms: 'bigdata:project:dql:monitor:list',
                  needLogin: true,
                  menuIndex: 'apiMonitor',
                  title: 'API监控'
                }
              }
            ]
          },
          {
            name: 'accessToken',
            path: 'accessToken',
            component: () => import('@/page/Project/Layout/AccessToken'),
            meta: {
              needLogin: true,
              menuIndex: 'accessToken',
              title: '接口令牌',
              icon: 'project_menu_token',
              perms: 'bigdata:project:third:client:list'
            }
          }
        ]
      }
    ]
  },

  {
    name: 'nodes',
    path: '/nodes',
    component: () => import('@/page/Nodes'),
    redirect: {
      name: 'innnerOpe'
    },
    meta: {
      needLogin: true,
      menuIndex: 'nodes',
      title: '算子管理'
    },
    children: [
      {
        name: 'nodeLayout',
        path: 'nodeLayout',
        component: () => import('@/page/Nodes/Layout'),
        meta: {
          needLogin: true,
          menuIndex: 'nodeLayout',
          title: '项目管理'
        },
        children: [
          {
            name: 'innnerOpe',
            path: 'innnerOpe',
            component: () => import('@/page/Nodes/Inner'),
            meta: {
              needLogin: true,
              menuIndex: 'innnerOpe',
              title: '内置算子',
              icon: 'project_menu_token'
            }
          },
          {
            name: 'definedOpe',
            path: 'definedOpe',
            component: () => import('@/page/Nodes/Defined'),
            meta: {
              needLogin: true,
              menuIndex: 'definedOpe',
              title: '自定义算子',
              icon: 'project_menu_token'
            }
          }
        ]
      }
    ]
  },


  // {
  //   name: 'notebook',
  //   path: '/notebook/:uniq?',
  //   component: () => import('../page/Workspace'),
  //   meta: {
  //     needLogin: true,
  //     menuIndex: 'notebook'
  //   }
  // },
  {
    name: 'jobs',
    path: '/jobs',
    component: () => import('@/page/Jobs'),
    meta: {
      needLogin: true,
      menuIndex: 'jobs',
      title: '任务管理',
      perms: 'bigdata:job:list'
    }
  },
  {
    name: 'schedules',
    path: '/schedules',
    component: () => import('@/page/Schedules'),
    meta: {
      needLogin: true,
      menuIndex: 'schedules',
      title: '调度管理',
      perms: 'bigdata:schedule:list'
    }
  },
  // {
  //   name: 'nodes',
  //   path: '/nodes',
  //   component: () => import('@/page/Nodes'),
  //   meta: {
  //     needLogin: true,
  //     menuIndex: 'nodes',
  //     title: '算子管理'
  //   }
  // },
  {
    name: 'resource',
    path: '/resource',
    component: () => import('@/page/Resource'),
    meta: {
      needLogin: true,
      menuIndex: 'resource',
      title: '资源管理',
      perms: 'bigdata:resource:list'
    }
  },
  // {
  //   name: 'systemSetting',
  //   path: '/systemSetting',
  //   component: () => import('@/page/SystemSetting'),
  //   meta: {
  //     needLogin: true,
  //     menuIndex: 'systemSetting',
  //     title: '系统设置',
  //     perms: 'bigdata:system'
  //   }
  // }
]
import store, { actionsTypes } from '../store'
import { MessageBox } from 'element-ui'
import router from './index'
// const filteredRoutes = filterRoutes(routes)
import { filterRoutes, filterPermissons } from './filterRoutes'
import { user } from '../service'

function getToken() {
  return window.localStorage.getItem('BCM-T')
}

export default async function beforeEach(to, from, next) {
  console.log('t-', to);
  let { query, path } = to;
  const propertyNames = Object.getOwnPropertyNames(query);
  const ticket = propertyNames.includes("BCM-T") || window.localStorage.getItem('BCM-Tk');

  console.log("ticket-", ticket, query["BCM-T"]);
  if (ticket) {
    window.localStorage.removeItem('BCM-T');
    let t = query["BCM-T"] || window.localStorage.getItem('BCM-Tk');
    localStorage.setItem("BCM-T", t);
  }

  console.log("token--", getToken());

  // 有token
  if (getToken()) {
    // 调取后台 获取权限集合
    if (store.state.user.userRoutes.length === 0 || !store.state.user.userInfo) {
      user.getUserAuth({ params: { appCode: 'bcm_platform' } }).then(res => {
        const allowAuth = filterRoutes(res.data)
        const permissons = filterPermissons(res.data)
        router.addRoutes(
          allowAuth
        )
        store.commit('SET_USER_ROUTE', allowAuth)
        store.commit('SET_USER_PERMISSONS', permissons)

        store.dispatch(actionsTypes.GET_USER_INFO).then(res => {
          let data = res.result
          store.commit('SET_USER_INFO', data)
          if (to.path == '/') {
            next({ path: allowAuth[0].path })
          } else {
            next({ ...to })
          }
        })
      })
      // store.dispatch(actionsTypes.GET_USER_INFO).then(res => {
      //   let data = res.result
      //   store.commit('SET_USER_INFO', data)
      //   // next({ ...to })
      // })
    } else {
      next()
    }
  } else {
    // const propertyNames = Object.getOwnPropertyNames(query);
    // const ticket = propertyNames.includes("BCM-T");
    // if (ticket) {
    //   let t = query["BCM-T"];
    //   localStorage.setItem("BCM-T", t);
    //   next({ path: "/" });
    // } else {
    MessageBox.alert('登录状态已过期，请重新登录', '系统提示', {
      confirmButtonText: '确定',
      type: 'warning',
      callback: action => {
        console.log(action);
      }
    })
    next()
    // }
  }
}




// import store, { actionsTypes } from '../store'

// export default async function beforeEach(to, from, next) {
//   try {
//     const res = await store.dispatch(actionsTypes.GET_ENV)
//     store.commit('SET_CURRENT_ENV', res.data.is_trial)
//     store.commit('SET_SCHEDULE_ENABLE', res.data.is_scheduler_enabled)
//     document.title = '全链路观测平台'
//   } catch (e) {
//     console.log(e)
//   }
//   if (store.state.user.userInfo) {
//     next()
//   } else if (to.meta.needLogin) {
//     store.dispatch(actionsTypes.GET_USER_INFO).then(res => {
//       let data = res.data
//       store.commit('SET_USER_INFO', data)
//       next({ ...to })
//     })
//   } else {
//     next()
//   }
// }
